package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.UserDao;
import com.example.demo.pojo.CarGood;
import com.example.demo.pojo.User;

@RestController
public class UserController {
	@Autowired
	UserDao userDao;
	
	@PostMapping("/login")
	public Map login(@RequestBody User user) {
		System.out.println(user+" "+user.getEmail()+"  "+user.getTel());
		String result="username no";
		List<User> mailUsers=null;
		List<User> telUsers=null;
		User resultUser=null;
		if(user.getEmail()==null) {
			System.out.println("tel");
			telUsers=userDao.findByTel(user.getTel());
			if(telUsers.isEmpty()) {
				result="tel no";
			}
			System.out.println(telUsers);
			for(User u:telUsers) {				
				if(u.getPassword().equals(user.getPassword())) {
					result="yes";
					resultUser=u;
				}
				else result="password no";
			}
		}
		else if(user.getTel()==null) {
			System.out.println("mail");
			mailUsers=userDao.findByEmail(user.getEmail());
			if(mailUsers.isEmpty()) {
				result="mail no";
			}
			for(User u:mailUsers) {
				if(u.getPassword().equals(user.getPassword())) {
					result="yes";
					resultUser=u;
				}
				else result="password no";
			}
		}
		Map map=new HashMap();
		map.put("result", result);
		map.put("user", resultUser);
		System.out.println(map.toString());
		return map;
	}
	
	@PostMapping("/register")
	public Map<String, String> register(@RequestBody User user) {
		String result="username no";
		List<User> mailUsers=null;
		List<User> telUsers=null;
		if(user.getEmail()==null) {
			telUsers=userDao.findByTel(user.getTel());
			if(telUsers.isEmpty()) {
				userDao.save(user);
				result="yes";
			}
			else result="tel no";
		}
		else if(user.getTel()==null) {
			mailUsers=userDao.findByEmail(user.getEmail());
			if(mailUsers.isEmpty()) {
				userDao.save(user);
				result="yes";
			}
			else result="mail no";
		}
		else {
			telUsers=userDao.findByTel(user.getTel());
			mailUsers=userDao.findByEmail(user.getEmail());
			System.out.println(telUsers+"\n"+mailUsers);
			if(telUsers.isEmpty()) {
				if(mailUsers.isEmpty()) {
					userDao.save(user);
					result="yes";
				}
				else result="mail no";
			}
			else result="tel no";
		}
			
		Map<String,String> map=new HashMap<String,String>();
		map.put("result", result);
		System.out.println(map.toString());
		return map;
	}
	
	@PostMapping("/changeUser")
	public Map<String,String> changeUser(@RequestBody User user){
		Map<String,String> map=new HashMap<String, String>();
		try {
			System.out.println(user.toString());
			userDao.save(user);
			map.put("result", "yes");
		}catch(Exception e) {
			map.put("result", "no");
			throw e;
		}
		return map;
	}
    @GetMapping("/test")
    public String test() {
        return "test";
    }
}
